<template>
  <div class="container">
    <el-row class="main-content" justify="center" type="flex">
      <!-- 申报表单 -->
      <el-col class="main-content-left" :span="6">
        <el-card class="form-card">
          <h2 class="form-title" style="text-align: center">非遗项目申报</h2>
          <el-form :model="form" label-width="80px" :rules="rules" ref="formRef">
            <el-form-item label="项目名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入非遗项目名称"/>
            </el-form-item>

            <el-form-item label="项目描述" prop="description">
              <el-input v-model="form.description" placeholder="请输入非遗项目描述"/>
            </el-form-item>

            <el-form-item label="项目分类" prop="categoryId">
              <el-select style="width: 100%" v-model="form.categoryId" placeholder="请选择分类">
                <el-option
                    v-for="item in CultureCategory"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"/>
              </el-select>
            </el-form-item>

            <el-form-item label="所在地区" prop="location">
              <el-cascader
                  style="width: 100%"
                  v-model="form.location"
                  :options="pcaTextArr()"
                  :props="{ value: 'value', label: 'label', children: 'children' }"
                  placeholder="请选择省/市/区"
              ></el-cascader>
            </el-form-item>

            <el-form-item label="项目资料">
              <el-upload
                  class="upload-demo"
                  :action="uploadTarget + '/common/files/upload'"
                  :on-remove="handleRemove"
                  :on-success="handleUploadSuccess"
                  :file-list="fileList"
                  list-type="picture">
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
              </el-upload>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="handleSubmit">立即申报</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>

      <!-- 已申报列表 -->
      <el-col class="main-content-right" :span="14">
        <el-card>
          <h2 class="list-title" style="text-align: center">已申报项目</h2>
          <el-form :model="queryParams" size="small" :inline="true" label-width="90px">
            <el-form-item label="项目名">
              <el-input v-model="queryParams.name" placeholder="项目名"></el-input>
            </el-form-item>
            <el-form-item label="项目描述">
              <el-input v-model="queryParams.description" placeholder="项目描述"></el-input>
            </el-form-item>
            <el-form-item label="项目类别">
              <el-select v-model="queryParams.categoryId" placeholder="请选择项目类别" clearable>
                <el-option
                    v-for="dict in CultureCategory"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
            <el-form-item style="display: flex;justify-content: center">
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
          <dv-scroll-board :config="config" style="height:625px;color: black;width: 100%"/>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {addCulture, queryCulture, queryCultureCategory} from "@/api/request/CultureDeclareRequest";
import {pcaTextArr} from "element-china-area-data";
import request from "@/utils/request";

export default {
  data() {
    return {
      queryParams: {
        name: null,
        description: null,
        categoryId: null
      },
      config: {
        header: ['名称', '描述', '类别', '地点', '状态', '创建时间'],
        data: [],
        index: true,
        columnWidth: [50],
        align: ['center'],
        headerBGC: '#cccccc',
        oddRowBGC: '#ffffff',
        evenRowBGC: '#f6f8fa',
        rowNum: 10,
        headerHeight: 50,
      },
      uploadTarget: process.env.VUE_APP_BASEURL,
      CultureCategory: [],
      avatar: require('@/assets/image/no_image.png'),
      loading: false,
      form: {
        name: null,
        description: null,
        categoryId: null,
        location: null,
        imgUrl: null
      },
      categories: [
        {id: 1, name: '传统技艺'},
        {id: 2, name: '民间文学'}
      ],
      regions: [
        {
          value: '北京市',
          children: [
            {
              value: '市辖区',
              children: [
                {value: '东城区'},
                {value: '西城区'}
              ]
            }
          ]
        }
      ],
      fileList: [],
      declaredItems: [],
      rules: {
        name: [
          {required: true, message: '请输入项目名', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入项目描述', trigger: 'blur'}
        ],
        categoryId: [
          {required: true, message: "请选择项目分类", trigger: "blur"}
        ],
        location: [
          {required: true, message: "请选择地址", trigger: "blur"}
        ]
      },
    }
  },
  methods: {
    pcaTextArr() {
      return pcaTextArr
    },
    getList() {
      this.loading = true

      queryCultureCategory().then(category => {
        if (category.code !== 200) {
          this.$message.error(category.msg);
          return;
        }
        this.CultureCategory = category.data

        queryCulture(this.queryParams).then(res => {
          if (res.code !== 200) {
            this.$message.error(res.msg);
            return;
          }
          this.loading = false

          // 使用 map 方法创建新的数据数组
          const data = res.data.map(item => ({
            name: item.name,
            description: item.description,
            category: category.data.find(d => d.dictValue == item.categoryId)?.dictLabel || '默认值',
            location: item.location,
            status: item.status === 0 ? '<span style="color:green;">已通过</span>' : '<span style="color:red;">待审核</span>',
            createdAt: item.createdAt
          }));

          this.config = {
            ...this.config,
            data: data.map(item => Object.values(item))
          }
        }).catch(error => {
          console.log(error)
        });
      }).catch(error => {
        console.log(error)
      });
    },
    handleSubmit() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          if (this.fileList.length) {
            this.form.imgUrl = this.fileList.map(item => {
              return item.response.data
            })
            this.form.imgUrl = this.form.imgUrl.join(',')
          }

          this.form.location = this.form.location.join(',')
          addCulture(this.form).then(res => {
            if (res.code !== 200) {
              this.$message.error(res.msg);
              return;
            }

            this.$message.success('申报成功');
            this.resetForm()

          }).catch(error => {
            console.log(error)
          });
        }
      })
    },
    handleRemove(file, fileList) {
      this.fileList = fileList;
      request.delete(file.response.data).catch(error => {
        console.log(error)
      })
    },
    handleUploadSuccess(res, file, fileList) {
      this.fileList = fileList; // 更新 fileList
      if (res.code !== 200) {
        this.$message.error(res.msg);
      }
    },
    handleQuery() {
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        name: null,
        description: null,
        categoryId: null
      }
      this.getList();
    },
    resetForm() {
      this.form = {
        name: null,
        description: null,
        categoryId: null,
        location: null,
        imgUrl: null
      }
      this.fileList = []
      this.getList();
    }
  },
  mounted() {
    this.getList()
  }
}
</script>

<style>
.el-card {
  background: none !important;
}
</style>
<style lang="scss" scoped>
@import "@/assets/css/CultureDeclareView.css";
</style>
